package com.wxdc.nmgyt.nmgytwydc.common;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.google.gson.Gson;
import com.just.agentwebX5.AgentWebX5;
import com.wxdc.nmgyt.nmgytwydc.R;
import com.wxdc.nmgyt.nmgytwydc.activity.WebActivity;
import com.wxdc.nmgyt.nmgytwydc.activity.common.YdInfoCommon;
import com.wxdc.nmgyt.nmgytwydc.utils.SharedPreferenceUtil;

/**
 * Created by cenxiaozhong on 2017/5/14.
 * source code  https://github.com/Justson/AgentWeb
 */

public class AndroidInterface {
    private Handler deliver = new Handler(Looper.getMainLooper());
    private AgentWebX5 agent;
    private WebActivity mWebActivity;
    private Context context;
    private Gson mGson = new Gson();
    public AndroidInterface(AgentWebX5 agent, Context context, WebActivity mActivity) {
        this.agent = agent;
        this.mWebActivity = mActivity;
        this.context = context;
    }

    /*
    * 测试Demo  js调用android方法保存数据
    * */
    @JavascriptInterface
    public void callAndroidSaveData(final String jsonStr) {
        deliver.post(new Runnable() {
            @Override
            public void run() {
                Log.i("TAG", "callAndroidSaveData: ---------------------------->" + jsonStr);
                Toast.makeText(context, "" + jsonStr, Toast.LENGTH_LONG).show();
            }
        });
        Log.i("Info", "Thread:" + Thread.currentThread());
    }
    //---------------------------------------------------------------------------------------------------
    /*
    *提示数据保存对话框
    * */
    @JavascriptInterface
    public void callAndroidSaveShowDialog(final String dataInfoStr) {
        deliver.post(new Runnable() {
            @Override
            public void run() {
                mWebActivity.showSaveDialog(dataInfoStr);
            }
        });
    }
    /*
    *提示数据提交对话框
    * */
    @JavascriptInterface
    public void callAndroidCommitShowDialog(final String dataInfoStr) {
        deliver.post(new Runnable() {
            @Override
            public void run() {
                mWebActivity.ShowCommitDialog(dataInfoStr);
            }
        });
    }
    /*
        *提示据删除对话框
        * */
    @JavascriptInterface
    public void callAndroidRemoveShowDialog(final String cbyfWzinfoStr,final int id) {
        deliver.post(new Runnable() {
            @Override
            public void run() {
                mWebActivity.showRemoveDialog(cbyfWzinfoStr,id);
            }
        });
    }
    /*
    * 数据提交反馈信息，js 调用android方法提示信息
    * */
    @JavascriptInterface
    public void callAndroidHintInfo(final String hintStr) {
        deliver.post(new Runnable() {
            @Override
            public void run() {
                Log.i("TAG", "callAndroidSaveData: ---------------------------->" + hintStr);
                Toast.makeText(context, "" + hintStr, Toast.LENGTH_LONG).show();
            }
        });
        Log.i("Info", "Thread:" + Thread.currentThread());
    }

    /*
    * 样地信息表中打开相册或相机
    * */
    @JavascriptInterface
    public void callAndroidOpenCameraAndAlbum(final int selectPicNum){
        deliver.post(new Runnable() {
            @Override
            public void run() {
                mWebActivity.openAlbumAndCamera(selectPicNum);
            }
        });
    }
    /*
    * web页面初始化完成
    * */
    @JavascriptInterface
    public void callAndroidWebViewInitComplete(){
        deliver.post(new Runnable() {
            @Override
            public void run() {
                mWebActivity.webViewInitComplete();
            }
        });
    }
    /*
    *
    * */
    @JavascriptInterface
    public void callAndroidHtmlPageDateInfo(final String htmlPageDateInfo,final boolean isYszPic) {
        deliver.post(new Runnable() {
            @Override
            public void run() {
                mWebActivity.getHtmlPageDateInfo(htmlPageDateInfo,isYszPic);
            }
        });
    }

    /**
     * 新增物种
     * @param dataInfoStr 当前页面所填信息
     */
    @JavascriptInterface
    public void callAndroidAddWzFunc(final String dataInfoStr){
        deliver.post(new Runnable() {
            @Override
            public void run() {
                mWebActivity.jumpAddWzActivity(dataInfoStr);
            }
        });
    }
    /**
     * 查询当前优势种是否有图片
     * @param wzmc
     */
    @JavascriptInterface
    public void callAndroidQueryLocalYszPic(final String wzmc){
        deliver.post(new Runnable() {
            @Override
            public void run() {
                mWebActivity.getCurtYszPic(wzmc);
            }
        });
    }

    /**
     * 接收物种图片地址
     * @param infoStr
     */
    @JavascriptInterface
    public void callAndroidWzPicUploadFun(final String infoStr){
        deliver.post(new Runnable() {
            @Override
            public void run() {
                mWebActivity.uploadpic(infoStr);
            }
        });
    }

    /**
     * 样地信息表中的动态标签数据
     * @return
     */
    @JavascriptInterface
    public String callAndroidYdHtmlElement(){
        String strData = mGson.toJson(YdInfoCommon.getInstance(context).getDynamicHtml());
        Log.i("TAG", "callAndroidYdHtmlElement: -------->"+strData);
        return  strData;
    }

    /**
     * 获取当前用户的token
     * @return
     */
    @JavascriptInterface
    public String callAndroidCurtUserToken(){
        String tokenStr = SharedPreferenceUtil.getToken();
        return tokenStr;
    }

    /**
     * 上传图片
     * @param picUrls
     */
    @JavascriptInterface
    public void callAndroidUploadPic(final String picUrls){
        deliver.post(new Runnable() {
            @Override
            public void run() {
                mWebActivity.uploadpic(picUrls);
            }
        });

    }
    /**
     * 显示js中的log日志
     * @param msg
     */
    @JavascriptInterface
    public void callAndroidShowLog(final String msg){
        Log.i("TAG", "callAndroidShowLog: ----jsLog--------->"+msg);
    }
}
